package czh.cocoa.ui.gesture;

import czh.cocoa.foundation.NSSet;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.ui.UIEvent;
import czh.cocoa.ui.UIGestureRecognizer;
import czh.cocoa.ui.UITouch;

public class UITapGestureRecognizer extends UIGestureRecognizer {

	private final static float TAP_MOVED_MAX = 20;
	
	private CGPoint mBeganPoint;
	
	public UITapGestureRecognizer() {
		mBeganPoint = CGPointMake(0, 0);
	}
	
	public void touchesBeganWithEvent(NSSet touches, UIEvent event) {
		UITouch touch = (UITouch) touches.anyObject();
		mBeganPoint = touch.locationInView(view());
		setState(UIGestureRecognizerStatePossible);
	}
	
	public void touchesMovedWithEvent(NSSet touches, UIEvent event) {
		UITouch touch = (UITouch) touches.anyObject();
		CGPoint moved_point = touch.locationInView(view());
		if (Math.abs(mBeganPoint.x - moved_point.x) >= TAP_MOVED_MAX || 
				Math.abs(mBeganPoint.y - moved_point.y) >= TAP_MOVED_MAX) {
			
			setState(UIGestureRecognizerStateFailed);
			
		}
	}
	
	public void touchesEndedWithEvent(NSSet touches, UIEvent event) {
		setState(UIGestureRecognizerStateEnded);
	}
	
	public void touchesCancelledWithEvent(NSSet touches, UIEvent event) {
		setState(UIGestureRecognizerStateFailed);
	}
	
}
